<%@Page Language="VB" %>
<%@Import Namespace="System.Xml" %>
<%@Import Namespace="System.Xml.Xpath" %>

<html><head>
<title>Accessing XML documents using an XPathNavigator</title>
</head>
<body bgcolor="#ffffff">
<span class="heading">
Accessing XML documents using an XPathNavigator</span><hr />
<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server">&nbsp;</div>
<div id="outResults" runat="server"></div>

<script language="vb" runat="server">

Sub Page_Load()

   Dim strCurrentPath As String = Request.PhysicalPath
   Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "booklist.xml"
   Dim objXMLDoc As New XmlDocument
   Try
      objXMLDoc.Load(strXMLPath)
      outDocURL.innerHTML = "Loaded file: <b>" & strXMLPath & "</b>"

   Catch objError As Exception
      outError.innerHTML = "<b>* Error while accessing document</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  

   End Try
   Dim objXPNav As XPathNavigator = objXMLDoc.CreateNavigator()
   objXPNav.MoveToRoot()
   outResults.innerHTML = GetXMLDocFragment(objXPNav, 0)

End Sub


Function GetXMLDocFragment(objXPNav As XPathNavigator, intLevel As Integer) As String

  Dim strNodes As String = ""
  Dim intLoop As Integer

  strNodes = strNodes & GetIndent(intLevel) _
           & GetNodeType(objXPNav.NodeType) & ": <b>" & objXPNav.Name _
           & "</b>&nbsp; value=<b>" & objXPNav.Value & "</b><br />"

  If objXPNav.HasAttributes Then

     objXPNav.MoveToFirstAttribute()

     Do
       strNodes = strNodes & GetIndent(intLevel + 1) _
                & GetNodeType(objXPNav.NodeType) & ": <b>" & objXPNav.Name _
                & "</b>&nbsp; value=<b>" & objXPNav.Value & "</b><br />"

     Loop While objXPNav.MoveToNextAttribute()
     objXPNav.MoveToParent()

  End If

  If objXPNav.HasChildren Then

     objXPNav.MoveToFirstChild()

     Do
       strNodes = strNodes & GetXMLDocFragment(objXPNav, intLevel + 1)
     Loop While objXPNav.MoveToNext()

     objXPNav.MoveToParent()

  End If

  Do While objXPNav.MoveToNext()

     strNodes = strNodes & GetXMLDocFragment(objXPNav, intLevel)

  Loop

  Return strNodes  'pass the result back to the caller

End Function


Function GetIndent(intLevel As Integer)
  'returns a string of non-breaking spaces used to indent each line
  Dim strIndent As String = ""
  Dim intIndent As Integer
  For intIndent = 0 To intLevel
    strIndent +=  "&nbsp; &nbsp; &nbsp; "
  Next
  Return strIndent
End Function

Function GetNodeType(intType As Integer) As String
   Select Case (intType)
     Case 0: Return "NONE"
     Case 1: Return "ELEMENT"
     Case 2: Return "ATTRIBUTE"
     Case 3: Return "TEXT"
     Case 4: Return "CDATA SECTION"
     Case 5: Return "ENTITY REFERENCE"
     Case 6: Return "ENTITY"
     Case 7: Return "PROCESSING INSTRUCTION"
     Case 8: Return "COMMENT"
     Case 9: Return "DOCUMENT"
     Case 10: Return "DOCUMENT TYPE"
     Case 11: Return "DOCUMENT FRAGMENT"
     Case 12: Return "NOTATION"
     Case 13: Return "WHITESPACE"
     Case 14: Return "SIGNIFICANT WHITESPACE"
     Case 15: Return "END ELEMENT"
     Case 16: Return "END ENTITY"
     Case 17: Return "XML DECLARATION"
     Case 18: Return "NODE (ALL)"
     Case Else: Return "UNKNOWN"
   End Select
End Function
</script>
</body>
</html>
